package com.example.bzh.gamecat.ui.society.allsociety;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.bean.AllGuildListBean;
import com.example.bzh.gamecat.config.HttpUrl;
import com.example.bzh.gamecat.ui.society.SecondActivity;
import com.example.bzh.gamecat.util.RoundImageUtil;

import java.util.List;

/**
 * Created by MyPC on 2016/8/16.
 */
public class AllSocietyAdapter extends BaseAdapter{

    private List<AllGuildListBean> list;
    private Context context;

    public AllSocietyAdapter(List<AllGuildListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, final ViewGroup viewGroup) {

        final ViewHolder holder;

        if (view == null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.allsociety_list_item,null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        final AllGuildListBean allGuildListBean = list.get(position);
        holder.tvName.setText(allGuildListBean.getName());
        holder.tvContent.setText(allGuildListBean.getDeclaration());
        holder.tvLevel.setText("LV."+allGuildListBean.getCatLevel());
        holder.tvId.setText("ID:"+allGuildListBean.getMId());
        holder.btnIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("入驻".equals(holder.btnIn.getText())){
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("确定加入群？");
                    builder.setNegativeButton("取消",null);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            holder.btnIn.setText("进入");
                            holder.btnIn.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.btn_all_society_press));
                        }
                    });
                    builder.show();
                }
            }
        });

        holder.llContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, SecondActivity.class);
                intent.putExtra("weburl", HttpUrl.getSocietyContentUrl(allGuildListBean.getMId()));
                intent.putExtra("title","公会宣传页");
                context.startActivity(intent);
            }
        });

        Glide.with(viewGroup.getContext()).load(allGuildListBean.getIconUrl()).transform(new RoundImageUtil.GlideRoundTransform(context,5)).into(holder.ivLogo);
        return view;
    }

    class ViewHolder{
        private ImageView ivLogo;
        private TextView tvName,tvContent,tvId,tvLevel;
        private Button btnIn;
        private LinearLayout llContent;
        public ViewHolder(View view){
            ivLogo = (ImageView) view.findViewById(R.id.iv_logo_item);
            tvName = (TextView) view.findViewById(R.id.tv_name_item);
            tvContent = (TextView) view.findViewById(R.id.tv_content_item);
            tvId = (TextView) view.findViewById(R.id.tv_id_item);
            tvLevel = (TextView) view.findViewById(R.id.tv_level_item);
            btnIn = (Button) view.findViewById(R.id.btn_all_society_item);
            llContent = (LinearLayout) view.findViewById(R.id.ll_society_content);
        }


    }


}
